<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="com.sz.common.ICommonDAO" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//String style1 = "width: 200px;text-align:right";
//String style2 = "text-align:left;width: 250px";


String style1 = "width: 80;text-align:right;font-size: 12px;";
String style2 = "width: 120px";
String style3 = "color: red;text-align:left;";
String style4 = "font-size: 12px;";

ServletContext servletContext = request.getSession().getServletContext(); 
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);

String tmp = null;
tmp = request.getParameter("region");
String region = (tmp!=null) ? tmp : "";
String create_manager = request.getSession().getAttribute("userid").toString();
String region_id = request.getSession().getAttribute("regionid").toString();

ICommonDAO dao = (ICommonDAO) context.getBean("CommonDAO");
String Region_sql = "select region_id,REGION_NAME from region where parent_id = " + region_id;
List list_Area = dao.queryForList(Region_sql);
%>

<%
//根据登陆区域查商圈
StringBuffer buf = new StringBuffer();
buf.delete(0, buf.length());
buf.append("select * from business_group t where t.region_id in (");
buf.append("select region_id from region start with region_id = ");
buf.append(region_id);
buf.append("connect by  parent_id = prior region_id)");
List list_business_group = dao.queryForList(buf.toString());
 %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	    <link href="<%= request.getContextPath() %>/css/main.css" rel="stylesheet" type="text/css" />
		<base href="<%=basePath%>">

		<title>查询订单</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="addTicket">
		<meta http-equiv="description" content="查询订单">
		
		<style type="text/css"> 
			@import "js/tableview/TableView.css";
    	</style>
	</head>
	
	<body>
		<script type="text/javascript" src="js/calendar.js"></script>
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/tableview/SortView.js"></script>
		<script type="text/javascript" src="js/tableview/PagerView.js"></script>
		<script type="text/javascript" src="js/tableview/TableView.js"></script>
		<script type="text/javascript" src="js/tableview/SelectorView.js"></script>
		<script type="text/javascript" src="js/tableview/PaperView.js"></script>
		<script type="text/javascript" src="js/utility.js"></script>
		<table width="100%" height="50px" border="0" cellpadding="0" cellspacing="0">
	  	 
	  	   		<tr>
		  	<td class="title0">商家信息维护<br></td>
		  	
	  	</tr>
	  	</table>
	  	
		<table width="100%" height=120px border="0" cellpadding="0" cellspacing="0">
			<tr>
			  	<td style="<%=style1%>">行政区&nbsp;</td>
		  	<td>
		  		<select id="region" name="region" style="width:156px">
				  <option value="">请选择</option>
				   <%
				   String value = null;
				   String text = null;
				   boolean b = false;
				   %>
				   <%
				   for(int n = 0; n < list_Area.size(); ++n)
				   {    
					   Map map = (Map)list_Area.get(n);
					   value = map.get("REGION_ID").toString();
					   text = map.get("REGION_NAME").toString();
					   b = value.equals(region);
				   %>
				   <option <%if(b){%> selected="selected" <%}%>
				     value="<%=value%>"><%=text%>
				   </option>
				   <%}%>				   
				</select>
		  	</td>
			  	
			  	<td style="<%=style1%>">商圈 &nbsp;<br></td>
			  	<td>
					<select id="group_id" name="group_id"> 
				  		<option value="">请选择</option> 
				  		<%
						 for(int i = 0; i < list_business_group.size(); ++i) {
							Map map = (Map)list_business_group.get(i);
							value = map.get("GROUP_ID").toString();
							text = map.get("NAME").toString();												
						 %>
						  <option value="<%=value%>"> 
						  <%=text%> 
						  </option>
						 <%} %> 
			  		</select>
			  	</td>	
			  	<td style="<%=style1%>">商家ID&nbsp;<br></td>		
			  	<td>
			  		<input  type="text" id="business_id" name="business_id"  style="<%=style2%>">
			  	</td>	  	
		  	</tr>	

	  		<tr>
	  			<td style="<%=style1%>">商家名称&nbsp;</td>
			  	<td>
			  		<input  type="text" id="business_name" name="business_name"  style="<%=style2%>">
			  	</td>	
			  	<td style="<%=style1%>">商家编码&nbsp;</td>
			  	<td>
			  		<input type="text" id="business_code" name="business_code" style="<%=style2%>">
			  	</td>	
			    <td style="<%=style1%>">商家电话&nbsp;</td>
			  	<td>
			  		<input type="text" id="business_tel" name="business_tel" style="<%=style2%>">
			  	</td>			  	
		  	</tr>
		  	
		  	<tr>
		  		<td style="<%=style1%>">合同生效日期&nbsp;</td>
			  	<td style="width: 250px;">
					<input type="text" id="contract_time_eff_start" name="contract_time_eff_start" readonly="readonly" style="width: 95px;" onclick="calendar.show(this)" >
			  	    <label style="<%=style4%>">至</label>
			  	    <input type="text" id="contract_time_eff_end" name="contract_time_eff_end" readonly="readonly" style="width: 95px;" onclick="calendar.show(this)" >
			  	</td>
			  	
		  	    <td style="<%=style1%>">合同失效日期&nbsp;</td>
			  	<td style="width: 250px;">
					<input type="text" id="contract_time_exp_start" name="contract_time_exp_start" readonly="readonly" style="width: 95px;" onclick="calendar.show(this)" >
			  	<label style="<%=style4%>">至</label>
			  	    <input type="text" id="contract_time_exp_end" name="contract_time_exp_end" readonly="readonly" style="width: 95px;" onclick="calendar.show(this)" >
			  	</td>
			  	
			    <td style="<%=style4%>">签约状态 &nbsp;<br></td>
			  	<td>
			  	  <select id="contract_state" name="contract_state" style="width:156px">
			  	  <option >请选择</option>
				  <option value="1">已签约</option>
				  <option value="0">未签约</option>	   
				</select>
			  	</td>
	  		</tr>
	  		
	  	    <tr>
		  		<td style="<%=style1%>">录入时间&nbsp;</td>
			  	<td style="width: 250px;">
					<input type="text" id="input_time" name="input_time" readonly="readonly" style="width: 95px;" onclick="calendar.show(this)" >
			  	</td>
		  	   <td style="<%=style1%>">录入人&nbsp;</td>
			  	<td style="width: 250px;">
					<input type="text" id="input_user" name="input_user" style="">
			  	</td>	
		  		<td style="<%=style1%>">预定状态 &nbsp;<br></td>
			  	<td>
			  	  	<select id="prep_state" name="prep_state" style="width:156px">
				  		<option >请选择</option>
				  		<option value="1">可预定</option>
				  		<option value="0">不可预定</option>   
					</select>
			  	</td>
		 	</tr>
		 	<tr>
		 		<td style="<%=style1%>">营业状态 &nbsp;<br>
		 		</td>
			  	<td>
			  	  <select id="trade_state" name="trade_state" style="width:156px">
			  	  	<option >请选择</option>
				  	<option value="1">已营业</option>
				  	<option value="2">歇业</option>		   
				</select>
			  	</td>
			    <td style="<%=style1%>">核单模式&nbsp;<br></td>
			  	<td>
			  	  <select id="check_order_mode" name="check_order_mode" style="width:156px">
			  	  	<option >请选择</option>
				  	<option value="1">核单</option>
				  	<option value="0">不核单</option>			   
				</select>
			  	</td>
			  	<td style="<%=style1%>">公布状态&nbsp;<br></td>
			  	<td>
			  	  <select id="publish_state" name="publish_state" style="width:156px">
			  	  	<option >请选择</option>
				  	<option value="1">公布</option>
				  	<option value="0">不公布</option>			   
				</select>
			  	</td>
			  	<td class="title0">
            		<input type="button" id="btn" name="btn" value="查询商家" onclick="onClickQueryBusiness()">
            		  			</td>
            	<td class="title0">
            		<input type="button" id="btn" name="btn" value="新增商家" onclick="onClickAddBusiness()">
	  			</td>	
		 	</tr>
	  	</table>

	  	<div id="table_div" style= "width: 100%"></div>
		<div id="pager"></div>		
		<script type="text/javascript">
		
		var numPerPage = 10; //每页行数
		var paramValue = ""; //形如  "{STATUS:'1', CREATE_MANAGER:'1'}"
		var delparamValue = "";
		var methodCount = "queryBusinessCount";
		var methodPage = "queryBusinessPage";
		
		var table = new TableView('table_div');
		table.header = {
			BUSINESS_ID			: '商家ID',
			RED_FLAG_STR		: '商家插旗',
			BUSINESS_CODE		: '商家编码',
			NAME		        : '商家名称',
			TEL1		        : '商家电话',
			INPUT_TIME_STR  	: '录入时间',
			INPUT_USER      	: '录入人',
			WORK_STATUS_STR         : '营业状态',
			PREORDER_FLAG_STR       : '预约状态',
			CHECK_STATUS_STR  	: '签约状态',
			CHECK_ORDER_MODE_STR	: '核单模式'	,
			DETAIL				: '详细信息',
			MODIFY_BUSINESS				: '修改商家信息'	,
			DEL_BUSINESS        :'删除商家信息'
		};		
		table.customDefine = {
				BUSINESS_ID			: '',
				BUSINESS_CODE		: '',
				RED_FLAG_STR		: '',
				NAME		        : '',
				TEL1		        : '',
				INPUT_TIME_STR		    : '',
				INPUT_USER	        : '',
				WORK_STATUS         :  '',
			    PREORDER_FLAG_STR       :  '',
				CHECK_STATUS_STR		: '',
				CHECK_ORDER_MODE_STR    : '',
				DETAIL                  :	'',
				MODIFY_BUSINESS				 :	'',
				DEL_BUSINESS :''
		};
				
		table.dataKey = 'BUSINESS_ID';
		table.count = false;
		table.title = false;
		table.display.filter = false;
		table.display.pager = false;
		table.pager.size = numPerPage;
		table.display.multiple = false;
		var pager = new PagerView('pager');
		pager.onclick = onPagerClick;
		pager.itemCount = 0;
		pager.size = numPerPage;
		
		table.render();
		pager.render();

		function queryBusiness(pageNO, returnCount) {
  	  		var begin = (pageNO-1) * numPerPage + 1;
  	  		var end = pageNO * numPerPage;
  	  		var mc = returnCount==true ? methodCount : "";
  	  		var date = {
  	  			BEAN_METHOD 	: "BusinessBMO.queryBusiness",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: paramValue,
  	    	  	METHOD_COUNT 	: mc, //为空则不返回记录数
  	    	  	METHOD_PAGE 	: methodPage, //为空则不分页
  	    	  	PAGE_BEGIN 		: begin, //仅在METHOD_PAGE不为空时使用
  	    	  	PAGE_END 		: end
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: query_success,
				error: show_error
  	  		});

  			return true;
  			
  		}

  		function onPagerClick(pageNO) {
  			queryBusiness(pageNO, "FLASE");
  		}
  		
  		//刷新当前页
  		function refreshCurrentPage() {
  			queryBusiness(pager.index, "FLASE");
  		}

  		function query_success(data) {
  	  		var jdata = eval("(" + data + ")");
  	  		if(jdata.COUNT != null) { //有记录数返回  	  	  		
  	  	  		pager.itemCount = jdata.COUNT;
  	  	  		pager.size = numPerPage;  	  	  		
  	  	  		pager.render();
  	  		} 
		   var array = eval(jdata.LIST);
		   for(var i = 0; i < array.length; ++i) {
		   		//形如：<a href="javascript:" onclick="about()">modal</a>
		   		var url = "<a href=\"javascript:\" onclick=\"onBusinessDetail('";
		   		url += array[i].BUSINESS_ID;
		   		url += "')\">详细信息</a>";
		   		array[i].DETAIL = url;
		   		
		   	    url = "<input type=\"button\" value=\"修改\" onclick=\"OnModifyBusiness('";
		   		url += array[i].BUSINESS_ID;		  
		   		url += "')\" />";
		   		array[i].MODIFY_BUSINESS = url;
		   		
		        url = "<input type=\"button\" value=\"删除\" onclick=\"OnDelBusiness('";
		   		url += array[i].BUSINESS_ID;		  
		   		url += "')\" />";
		   		array[i].DEL_BUSINESS = url;
		   		
		   		
		   		//签约状态转义
		   		if(array[i].CHECK_STATUS ==1) array[i].CHECK_STATUS_STR = "已签约";
                else if(array[i].CHECK_STATUS ==0) array[i].CHECK_STATUS_STR = "未签约";
                else array[i].CHECK_STATUS_STR = "未知";
                
                //预约状态转义
                if(array[i].PREORDER_FLAG ==1) array[i].PREORDER_FLAG_STR = "可预定";
                else if(array[i].PREORDER_FLAG ==0) array[i].PREORDER_FLAG_STR = "不可预定";
                else array[i].PREORDER_FLAG_STR = "未知";
                
                //营业状态转义
                if(array[i].WORK_STATUS ==1) array[i].WORK_STATUS_STR = "正常";
                else if(array[i].WORK_STATUS ==2) array[i].WORK_STATUS_STR = "关闭";
                else array[i].WORK_STATUS_STR = "未知";
                
                //核单状态转义
                if(array[i].CHECK_ORDER_MODE ==1) array[i].CHECK_ORDER_MODE_STR = "已核单";
                else if(array[i].CHECK_ORDER_MODE ==0) array[i].CHECK_ORDER_MODE_STR = "未核单";
                else array[i].CHECK_ORDER_MODE_STR = "未知";
		   		
		   		//红旗商家转义
		   		if(array[i].RED_FLAG ==1) array[i].RED_FLAG_STR = "红旗";
                else if(array[i].RED_FLAG ==0) array[i].RED_FLAG_STR = "无旗";
                else array[i].RED_FLAG_STR = "未知";
		   }
		   table.clear();
		   table.addRange(array);
		}
  		function show_error(data) {
  			var err = "操作失败！\n";
  			err += "status：" + data.status + "\n";
  			err += "statusText：" + data.statusText;
  	  		alert(err);
 		}
 		
  		function onClickQueryBusiness() 
  		{		
  			var tmp;
			paramValue = "{";
			
  			tmp = document.getElementById("region").value;
  			paramValue += "  REGION_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("group_id").value;
  			paramValue += " GROUP_ID:'" + tmp;
  			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("business_id").value;
  			paramValue += " BUSINESS_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_name").value;
  			paramValue += " NAME:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_code").value.toUpperCase();
  			paramValue += " BUSINESS_CODE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("business_tel").value;
  			paramValue += " TEL1:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("contract_time_eff_start").value;
  			paramValue += " EFF_DATE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("contract_time_eff_end").value;
  			paramValue += " EFF_DATE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("contract_time_exp_start").value;
  			paramValue += " EXP_DATE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("contract_time_exp_end").value;
  			paramValue += " EXP_DATE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
		    tmp = document.getElementById("contract_state").value;
  			paramValue += "  CHECK_STATUS:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("input_time").value;
  			paramValue += " INPUT_TIME:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("input_user").value;
  			paramValue += " INPUT_USER:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
		    tmp = document.getElementById("prep_state").value;
  			paramValue += " PREORDER_FLAG:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
		    tmp = document.getElementById("trade_state").value;
  			paramValue += "  WORK_STATUS:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
		    tmp = document.getElementById("check_order_mode").value;
  			paramValue += " CHECK_ORDER_MODE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
		    tmp = document.getElementById("publish_state").value;
  			paramValue += " PUBLISH_STATE:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
  			paramValue += "}";
  				
  	  		pager.index = 1;
  	  		queryBusiness(1, true);
  		}
  		function onBusinessDetail(business_id) {
   			var url = "<%=path%>";
  			url += "/page/viewBusiness.jsp?business_id=" + business_id;
  			var param = "status:no;scroll:yes;dialogWidth:1024px;dialogHeight:600px";
			window.showModalDialog(url, window, param); 			
  		}
  		
  		 function onClickAddBusiness() {
  			var url = "<%=path%>";
  			url += "/business/addbusiness.do?";
  			url += "&title=" + encodeURI(encodeURI("新增商家信息"));
  			var param = "status:no;scroll:yes;dialogWidth:1024px;dialogHeight:700px";
			window.showModalDialog(url, window, param);
 		}
 		
 		function OnModifyBusiness(business_id) {
   			var url = "<%=path%>";
  			url += "/page/ModifyBusiness.jsp?business_id=" + business_id;
  			var param = "status:no;scroll:yes;dialogWidth:1024px;dialogHeight:600px";
			window.showModalDialog(url, window, param); 			
  		}

 		function showMsg(data) {
  	  		alert(data);
 		}

 		function OnDelBusiness(business_id) 
 		{
			var date = {
 	  			OPERATE_TYPE	: "DEL",
 	  			BUSINESS_ID 	: business_id
 	  	    };
 	  	    
 	  		$.ajax({
			type: "POST",
			url: "delBusiness.exec",
			//data: {id:'123'},
			data: date,
			datatype:"json",
			success: del_success,
			error: show_error
 	  		});	
 		}

        function del_success(data)
        {
	        alert(data);
	        refreshCurrentPage();
        }
	   
 		function onClickEdit() {
 			var rows = table.getSelected(); // Array[Object]
  			if(rows.length != 1) {
  				alert("请选择1 条订单");
  				return;
  			}
  			
  			url = "<%=path%>";
  			url += "/ticket/editTicket.do?ticket_id=" + rows[0].TICKET_ID;
  			url += "&title=" + encodeURI(encodeURI("编辑订单"));
  			var param = "status:no;scroll:no;dialogWidth:1024px;dialogHeight:700px";
			window.showModalDialog(url, window, param);
			refreshCurrentPage();
 		}
 		
	</script>
	
	</body>
</html>
